<template>
  <div class="cart-sku">
    <div class="attrs" @click="toggle">
      <span class="ellipsis">{{ attrsText }}</span>
      <i class="iconfont icon-angle-down"></i>
    </div>
    <div class="layer" v-if="visible" ref="target">
      <div class="loading" v-if="loadingSku"></div>
      <GoodsSku
        v-if="!loadingSku"
        :skuId="skuId"
        :specs="specsAndSkus.specs"
        :skus="specsAndSkus.skus"
        @on-spec-changed="onSpecChanged"
      />
      <XtxButton
        @click="submitSku"
        v-if="specsAndSkus"
        type="primary"
        size="mini"
        style="margin-left: 60px"
      >
        确定
      </XtxButton>
    </div>
  </div>
</template>
<script>
import { ref } from "vue";
import { onClickOutside } from "@vueuse/core";
import { getSkuInfoBySkuId } from "@/api/cart";
import GoodsSku from "@/views/goods/components/GoodsSku";
import Message from "@/components/library/Message";
import { useStore } from "vuex";

export default {
  name: "CartSku",
  components: { GoodsSku },
  props: {
    attrsText: {
      type: String,
      default: "",
    },
    skuId: {
      type: String,
    },
  },

  setup(props) {
    const visible = ref(false);
    const target = ref();
    // 数据加载状态
    const loadingSku = ref(false);
    // 保存规格可选数据
    const specsAndSkus = ref();
    const store = useStore();
    // 隐藏规格选择组件
    const hide = () => {
      visible.value = false;
    };
    // 显示规格选择组件
    const show = async () => {
      // 模态框显示
      visible.value = true;
      // 数据加载zhong
      loadingSku.value = true;
      // 获取规格可选择数据
      const data = await getSkuInfoBySkuId(props.skuId);
      specsAndSkus.value = data.result;
      // 数据加载完毕
      loadingSku.value = false;
    };
    const toggle = () => {
      visible.value ? hide() : show();
    };
    // 在组件外部点击隐藏组件
    onClickOutside(target, () => {
      visible.value && hide();
    });
    // 存储用户选择的新的商品规格
    let userSelectedNewSku = null;
    // 监听规格信息变化
    const onSpecChanged = (sku) => {
      userSelectedNewSku = sku;
    };
    // 提交规格
    const submitSku = () => {
      // 如果用户没有重新选择规格, 或者用户选择了规格, 但是选择的规格和之前是一样的
      if (
        !userSelectedNewSku ||
        (userSelectedNewSku && userSelectedNewSku.skuId === props.skuId)
      ) {
        // 用户提示
        Message({ type: "warn", text: "商品规格信息没有发生变化" });
      }
      store.dispatch("cart/updateGoodsOfCartBySkuChanged", {
        oldSkuId: props.skuId,
        userSelectedNewSku,
      });
      hide();
    };
    return {
      visible,
      toggle,
      target,
      loadingSku,
      specsAndSkus,
      onSpecChanged,
      submitSku,
    };
  },
};
</script>
<style scoped lang="less">
.cart-sku {
  height: 28px;
  border: 1px solid #f5f5f5;
  padding: 0 6px;
  position: relative;
  margin-top: 10px;
  display: inline-block;
  .attrs {
    line-height: 24px;
    display: flex;
    span {
      max-width: 230px;
      font-size: 14px;
      color: #999;
    }
    i {
      margin-left: 5px;
      font-size: 14px;
    }
  }
  .layer {
    position: absolute;
    left: -1px;
    top: 40px;
    z-index: 10;
    width: 400px;
    border: 1px solid @xtxColor;
    box-shadow: 2px 2px 4px lighten(@xtxColor, 50%);
    background: #fff;
    border-radius: 4px;
    font-size: 14px;
    padding: 20px;
    &::before {
      content: "";
      width: 12px;
      height: 12px;
      border-left: 1px solid @xtxColor;
      border-top: 1px solid @xtxColor;
      position: absolute;
      left: 12px;
      top: -8px;
      background: #fff;
      transform: scale(0.8, 1) rotate(45deg);
    }
    .loading {
      height: 224px;
      background: url(../../../assets/images/loading.gif) no-repeat center;
    }
  }
}
</style>
